package ui.activity.mall;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

import Dailog.PayDialog;
import base.BaseBean;
import base.activity.BaseNetActivity;
import bean.GoodsApply;
import bean.GoodsPay;
import event.MainPageEvent;
import student.andlei.com.student.R;
import utils.GsonUtils;
import utils.ImageLoaderUtils;

/**
 * author : ChenHuiMing
 * date : 2018/9/16
 * email : 907399255@qq.com
 * description : 立即报名
 */
public class MallApplyActivity extends BaseNetActivity {

    public static final String CREATE_ORDER_URL = "/Shop/createOrder";
    public static final String PAYMENT_URL = "/Shop/payment";
    public static final String EXTRA_SCHEDULE_ID = "extra_schedule_id";

    private ImageView mIvBook;
    private TextView mTvTeacher;
    private TextView mTvCourse;
    private TextView mTvTime;
    private TextView mTvHour;
    private TextView mTvPrice;
    private TextView mTvApply;

    private TextView mTvOrderNo;
    private TextView mTvOrderTime;

    private LinearLayout mLlAliPay;
    private LinearLayout mLlWeixinPay;
    private CheckBox mCbAliPay;
    private CheckBox mCbWeixinPay;

    private String mScheduleId;
    private GoodsApply mGoodsApply;
    private PayDialog mPayDialog;

    public static void startMallApplyActivity(Context context, String schedule_id) {
        Intent intent = new Intent(context, MallApplyActivity.class);
        intent.putExtra(EXTRA_SCHEDULE_ID, schedule_id);
        context.startActivity(intent);
    }

    @Override
    public void success(String action, BaseBean baseBean) {
        if (TextUtils.equals(action, CREATE_ORDER_URL)) {
            if (baseBean.getCode() == 200) {
                mGoodsApply = (GoodsApply) GsonUtils.JSONToObject(GsonUtils.beanToJSONString(baseBean.data), GoodsApply.class);
                mTvTeacher.setText("主讲：" + mGoodsApply.teacher_name);
                mTvCourse.setText(mGoodsApply.schedule_name);
                mTvTime.setText("时间：" + mGoodsApply.start_date + " - " + mGoodsApply.end_date);
                mTvHour.setText("课时：" + mGoodsApply.room_number+"节");
                mTvPrice.setText("￥" + mGoodsApply.price);
                mTvOrderNo.setText("订单号：" + mGoodsApply.order_no);
                mTvOrderTime.setText("订单生成时间：" + mGoodsApply.create_time);
                ImageLoaderUtils.loadImage(this, mGoodsApply.schedule_image, mIvBook);
            } else {
                toast(baseBean.getMsg());
                finish();
            }
        } else if (TextUtils.equals(action, PAYMENT_URL)) {
            GoodsPay goodsPay = (GoodsPay) GsonUtils.JSONToObject(GsonUtils.beanToJSONString(baseBean.data), GoodsPay.class);
            if (baseBean.getCode() == 200) {
                if (!TextUtils.isEmpty(goodsPay.qr_code_img)) {
                    if (mPayDialog != null && mPayDialog.isShowing()) {
                        mPayDialog.dismiss();
                    }
                    mPayDialog = new PayDialog(MallApplyActivity.this, goodsPay.qr_code_img, mGoodsApply.order_id);
                    mPayDialog.setOnPayListener(new PayDialog.OnPayListener() {
                        @Override
                        public void onPay(boolean isSucceed) {
                            if (isSucceed) {
                                //跳转到我的订单
                                MainPageEvent pageEvent = new MainPageEvent();
                                pageEvent.firstPage = MainPageEvent.MINE_PAGE;
                                pageEvent.secondPage = 1;
                                EventBus.getDefault().post(pageEvent);
                                toast("支付成功");
                                finish();
                            }
                        }
                    });
                    mPayDialog.show();
                }
            } else {
                    Toast.makeText(MallApplyActivity.this, baseBean.getMsg(), Toast.LENGTH_LONG).show();
            }
        }
    }

    @Override
    public void error(String action, Throwable e) {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_mall_apply;
    }


    @Override
    protected void findViews() {
        mIvBook = getView(R.id.iv_book);
        mTvTeacher = getView(R.id.tv_teacher);
        mTvCourse = getView(R.id.tv_course);
        mTvTime = getView(R.id.tv_time);
        mTvHour = getView(R.id.tv_hour);
        mTvPrice = getView(R.id.tv_price);
        mTvApply = getView(R.id.tv_apply);
        mTvOrderNo = getView(R.id.tv_order_no);
        mTvOrderTime = getView(R.id.tv_order_time);
        mLlAliPay = getView(R.id.ll_ali_pay);
        mLlWeixinPay = getView(R.id.ll_wechat_pay);
        mCbAliPay = getView(R.id.cb_ali_pay);
        mCbWeixinPay = getView(R.id.cb_wechat_pay);
    }

    @Override
    protected void formatViews() {
        mTvApply.setOnClickListener(this);
        mLlAliPay.setOnClickListener(this);
        mLlWeixinPay.setOnClickListener(this);
        mCbAliPay.setOnClickListener(this);
        mCbWeixinPay.setOnClickListener(this);
    }

    @Override
    protected void formatData() {
        Map<String, Object> map = new HashMap<>();
        map.put("schedule_id", mScheduleId);
        post(CREATE_ORDER_URL, map, BaseBean.class, false);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void getBundle(Bundle bundle) {
        setTitle("确定订单");
        mScheduleId = getIntent().getStringExtra(EXTRA_SCHEDULE_ID);
    }

    @Override
    public void onClick(View view) {
        if (view == mTvApply && mGoodsApply != null) {
            Map<String, Object> map = new HashMap<>();
            map.put("order_id", mGoodsApply.order_id);
            map.put("order_type", 2);
            map.put("pay_type", mCbAliPay.isChecked() ? 2 : 1);
            post(PAYMENT_URL, map, BaseBean.class, false);
        } else if (view == mLlAliPay || view == mCbAliPay) {
            mCbAliPay.setChecked(true);
            mCbWeixinPay.setChecked(false);
        } else if (view == mLlWeixinPay || view == mCbWeixinPay) {
            mCbAliPay.setChecked(false);
            mCbWeixinPay.setChecked(true);
        }
    }
}
